﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace DesertEagleQuest
{
    class Minimap
    {

        #region Declarations 

        private const int MAPWIDTH = 150; 
        private const int MAPHEIGHT = 150;

        private const int DOTWIDHT = 12;
        private const int DOTHEIGHT = 12;

        private Vector2 MINIMAPSCREENPOSITION = new Vector2(1000, 500); 


        //The Dictionary is needed to store a unique string (Hash-Value) and the position of the dot.
        private Dictionary<string, Vector3> ScaledVectorDic = new Dictionary<string, Vector3>(); 

        private Vector3 origin = new Vector3(MAPWIDTH / 2, 0, MAPHEIGHT / 2);   
        private Vector3 shipPosition; 

        private Texture2D TextureMap;
        private Texture2D Dot;

        #endregion 


        #region Constructor

        public Minimap(Texture2D map, Texture2D dot)
        {
            TextureMap = map; 
            Dot = dot;           
        }

        #endregion 

        #region Update & Draw

        public void Update(GameTime gameTime, Vector3 vec)
        {
            UpdateShipPosition(vec);            
        }

        public void Draw(SpriteBatch spriteBatch)
        {
            spriteBatch.Draw(
                TextureMap,
                new Rectangle((int)MINIMAPSCREENPOSITION.X, (int)MINIMAPSCREENPOSITION.Y, MAPWIDTH, MAPHEIGHT), 
                Color.White);

            //Schiff zeichnen 

            spriteBatch.Draw(
               Dot,
               new Rectangle((int)MINIMAPSCREENPOSITION.X + (int)shipPosition.X, (int)MINIMAPSCREENPOSITION.Y + (int)shipPosition.Z, DOTWIDHT, DOTHEIGHT),
               Color.Black);

            List<Vector3> DicValues = new List<Vector3>(ScaledVectorDic.Values);
            
            foreach (Vector3 vec in DicValues)
            {
                spriteBatch.Draw(
                Dot,
                new Rectangle((int)MINIMAPSCREENPOSITION.X + (int)vec.X, (int)MINIMAPSCREENPOSITION.Y + (int)vec.Z, DOTWIDHT, DOTHEIGHT),
                Color.White);

            }
        }

        #endregion 

        #region HelperMethods

        private Vector3 scaleVector(Vector3 vec)
        {
            return  ((vec/80000)*new Vector3(MAPWIDTH/2, 0, MAPHEIGHT/2))+origin;
        }

        #endregion
       

        public void AddElement(string hashString, Vector3 vec)
        {            
            ScaledVectorDic.Add(hashString, scaleVector(vec));           
        }

        public void RemoveElement(string hashString)
        {            
            ScaledVectorDic.Remove(hashString); 
        }


        public void UpdateShipPosition(Vector3 vec)
        {
            shipPosition = scaleVector(vec); 
        }
    }
}
